//
//  CIBNChannelController.m
//  CIBN
//
//  Created by ZNGOO on 16/7/18.
//  Copyright © 2016年 韦儒健. All rights reserved.
//

#import "CIBNChannelController.h"
#import "CIBNMenuListModel.h"
#import "CIBNChannelCell.h"
#import "CIBNHomeCategoryController.h"



static NSString * channelCell = @"CIBNChannelCell";

@interface CIBNChannelController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic,strong) UICollectionView * collctionView;

/**  菜单数据 */
@property (nonatomic,strong) NSMutableArray * menuArray;

@end

@implementation CIBNChannelController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setItem];
    
    [self createCollectionView];
    
    [self loadMenuData];
}

- (void)createCollectionView{
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake((ScreenWidth - 2)/3, (ScreenWidth - 2)/3);
    layout.sectionInset = UIEdgeInsetsMake(10, 0, 0, 0);
    layout.minimumLineSpacing = 1;
    layout.minimumInteritemSpacing = 1;
    
    self.collctionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64 - 50) collectionViewLayout:layout];
    self.collctionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.collctionView.showsVerticalScrollIndicator = NO;
    self.collctionView.showsHorizontalScrollIndicator = NO;
    self.collctionView.delegate = self;
    self.collctionView.dataSource = self;
    [self.collctionView registerClass:[CIBNChannelCell class] forCellWithReuseIdentifier:channelCell];
    [self.view addSubview:self.collctionView];
}

#pragma mark -UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.menuArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CIBNChannelCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:channelCell forIndexPath:indexPath];
    CIBNMenuListModel * model = self.menuArray[indexPath.row];
    [cell setCellsModel:model];
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CIBNMenuListModel * model = self.menuArray[indexPath.row];
    CIBNHomeCategoryController * categoryVC = [[CIBNHomeCategoryController alloc] init];
    categoryVC.model = model;
    categoryVC.isH = indexPath.row + 1;
    [self.navigationController pushViewController:categoryVC animated:YES];
}

- (void)setItem{
    self.leftBtn.hidden = YES;
    self.rightBtn2.hidden = YES;
    [self.rightBtn1 setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    self.centerTitle.text = @"全部频道";
}


#pragma mark - 获取菜单
- (void)loadMenuData{
    
    self.menuArray = [[NSMutableArray alloc] init];
    
    [RJNetwoking POST:Domain_URL body:@{@"Cmdid":@"4609",
                                        @"Seqid":@"319",
                                        @"ProtoVer":@"1",
                                        @"UID":@"3000000001",
                                        @"HID":@"049364046517234524AF2C911F6D5228",
                                        @"TID":@"42756",
                                        @"CliVer":@"72058693549555712",
                                        @"Oemid":@"10001",
                                        @"Epgid":@"9",
                                        @"Sessid":@"10411839877922554998",
                                        @"TermType":@"30",
                                        @"SubjectType":@"1"
                                        } success:^(id  _Null_unspecified responseObject) {
                                            
                                            NSMutableArray * array = [[CIBNMenuListModel mj_objectArrayWithKeyValuesArray:responseObject[@"GetMenuList"]] mutableCopy];
                                            
                                            for (CIBNMenuListModel * model in array) {
                                                if ([model.Name isEqualToString:@"推荐"] || [model.Name isEqualToString:@"会员专区"] || [model.Name isEqualToString:@"开通会员"]) {

                                                }else{
            
                                                    [self.menuArray addObject:model];
                                                }
                                                
                                                
                                            }
                                            
                                            [self.collctionView reloadData];
                                            
                                        } failure:^(NSError * _Nonnull error) {
                                            
                                            [MBProgressHUD bwm_showTitle:@"出错啦" toView:self.view hideAfter:2.0 msgType:BWMMBProgressHUDMsgTypeError];
                                            
                                        }];
    
}


-(void)viewWillAppear:(BOOL)animated{
    [InterfacOrientation setInterfacOrientation:UIInterfaceOrientationPortrait];
    self.tabBarController.tabBar.hidden = NO;
}
@end
